from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
from user.myjwt import myjwt
import time, json
from django_redis import get_redis_connection
redis = get_redis_connection("url")

class PermitionMiddleWare(MiddlewareMixin):
    def process_request(self, request):
        #定义白名单
        wlist = [
            "/user/login/",
                 "/user/register/","/user/refresh_token/,/user/quit/","/","/admin/","/uploads/jiazheng/lihelogo.png"]
        url = request.path
        # print(url,"公共路径,,,,,,,,,")
        print(request.headers.get('Authorization'),'+++++++++++++++++7878979879')
        # if url not in wlist:
        #     if request.headers.get('Authorization'):
        #     #token转为字节类型
        #         token = request.headers.get('Authorization').encode('utf-8')
        #
        #         # 查询redis数据库,token为key判断是否存在,存在的话代表已经退出
        #         if redis.get(token):
        #             res = {
        #                 "msg": "用户已退出",
        #                 "code": 200
        #             }
        #             return JsonResponse(res)
        #
        #         print(token,"++===============")
        #         data = myjwt.jwt_decode(token)
        #         plist = redis.get(f'user{data["id"]}promition')
        #         print(plist,"权限列表")
        #         if url in json.loads(plist):
        #             if int(time.time()) < int(data['exp']):
        #                 #token还在有效期
        #                 print("token有效")
        #             else:
        #                 res = {
        #                     "msg": "token过期",
        #                     "code": 401
        #                 }
        #                 return JsonResponse(res)
        #         else:
        #             res = {
        #                 "msg": "没有权限",
        #                 "code": 403
        #             }
        #             return JsonResponse(res)
        #     else:
        #         res = {
        #             "msg": "请登录",
        #             "code": 401
        #         }
        #         return JsonResponse(res)
        # else:
        #     pass



